<template>
  <div class="custom-tab-page">
    <div class="tab4-title">
      <div class="left">
        <div style="margin-right: 20px">
          <label>合同总数：</label>
          <span> {{ statisticsInfo.num }} </span>
        </div>
        <div>
          <label>合同总金额：</label>
          <span> ￥{{ statisticsInfo.parice }} </span>
        </div>
      </div>
      <el-button v-if="showItem" class="right" type="primary" @click="addFun" size="small">
        + 添加
      </el-button>
    </div>
    <el-table :data="tableData" border style="width: 100%; margin-top: 15px" :height="store.tableHeight - 120">
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="order_no" label="合同编号" width="200" />
      <el-table-column prop="order_name" label="合同名称" width="150" />
      <el-table-column prop="amount_real" label="合同金额(元)" width="130" />
      <el-table-column prop="order_date" label="签署日期" width="170" />
      <el-table-column prop="pay_amount" label="已收款" />

      <el-table-column prop="a" label="合同状态" width="100">
        <template #default="{ row }">
          <el-tag v-if="row.status == 1">实施中</el-tag>
          <el-tag v-else-if="row.status == 2" type="warning">已完结</el-tag>
          <el-tag v-else-if="row.status == 3" type="success">已付全款</el-tag>
          <el-tag v-else-if="row.status == 0" type="danger">待开始</el-tag>
          <el-tag v-else-if="row.status == -1" type="info">无效</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="销售" />
      <el-table-column prop="created_at" label="录入时间"  width="180" />
      <el-table-column width="120" label="操作" align="center" fixed="right" v-if="showItem">
        <template #default="{ row }">
          <el-button type="primary" link @click="saveFun(row)">
            编辑
          </el-button>
          <el-button type="danger" link @click="delFun(row)"> 删除 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px; display: flex; justify-content: center">
      <el-pagination :small="true" v-model:currentPage="currentPage" v-model:page-size="pageSize"
        :page-sizes="[20, 50, 100, 200]" layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
  <addDialog :dataInfo="dataInfo" ref="addDialogRef" @refreshList="refreshList" />
  <!--  -->
</template>
<script setup>
import { listApi, delApi } from "@/plugins/api/customer/manage/tab4";
import { useCounterStore } from "@/stores/counter.js";
import addDialog from "./tabDialog/Tab4-dialog.vue";
// 合同信息

const store = useCounterStore();
const props = defineProps({
  dataInfo: {
    type: Object,
    default: {},
  },
  customInfo: {
    type: Object,
    default: {},
  },
  showItem: {
    type: Boolean,
    default: false,
  },
});

let tableData = ref([]);
// 分页
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};
let statisticsInfo = ref({
  num: 0,
  parice: 0,
});
function getData() {
  listApi({
    page: currentPage.value,
    pageSize: pageSize.value,
    customer_id: props.dataInfo.id,
  }).then((res) => {
    tableData.value = res.list;
    statisticsInfo.value.num = res.total;
    statisticsInfo.value.parice = res.total_money;
    total.value = res.total;
  });
}
function refreshList() {
  getData();
}
let addDialogRef = ref(null);
function addFun() {
  addDialogRef.value.initFun("add");
}
function saveFun(row) {
  addDialogRef.value.initFun("update", row);
}
function initFun() {
  tableData.value = [];
  getData();
}
defineExpose({
  initFun,
});
function delFun(row) {
  ElMessageBox.confirm("确认批量删除选中的数据，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi({
        id: row.id
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });

        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}
onMounted(() => {

});
</script>

<style lang="scss" scoped>
.custom-tab-page {
  width: 100%;

  .tab4-title {
    display: flex;
    justify-content: space-between;
    height: 46px;
    background: #fafbfc;
    box-sizing: border-box;
    padding-left: 10px;

    >div {
      align-self: center;
      margin-right: 20px;

      label {
        font-size: 14px;
        font-weight: 500;
        color: #5e6c84;
      }

      span {
        font-size: 14px;
        font-weight: 500;
        color: #005ecc;
      }
    }

    .left {
      display: flex;
    }

    .right {
      align-self: center;
      margin-right: 10px;
    }
  }
}
</style>
